package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.entity.College;
import com.orientation.service.CollegeService;
import com.orientation.vo.CollegeInfoVo;
import com.orientation.vo.CollegeMajorVo;
import com.orientation.vo.CollegeTreeVo;
import com.orientation.commom.Result;
import com.orientation.vo.ReserveCollegeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 学院 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */

@Api(tags = "学院")
@Validated//来校验数据，如果数据异常则会统一抛出异常，方便异常中心统一处理
@RestController
@RequestMapping("/college")
public class CollegeController {

    @Resource
    private CollegeService collegeService;

    @ApiOperation("新增学院数据")
// @PostMapping("/insertPileInfoDto")
    @PostMapping
    public ResponseEntity<Result> insertCollege(@RequestBody College college) {
        collegeService.insertCollege(college);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody College college) {
        return collegeService.updateById(college);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return collegeService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public College findById(@PathVariable String id) {
        return collegeService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<College> findAll() {
        return collegeService.list();
    }

    @ApiOperation("查询所有")
    @GetMapping("/getAll")
    public List<College> getAll() {
        return collegeService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseEntity<Result> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        // return collegeService.page(new Page<>(pageNum, pageSize), Wrappers.<College>lambdaQuery().like(StringUtils.isNotBlank(name), College::getCollegeName, name));
        IPage<College> collegeIPage = collegeService.getCollege(pageNum, pageSize, name);
        return ResponseEntity.ok(new Result(collegeIPage));

    }

    @ApiOperation("不同条件筛选男女信息统计/废弃")
    @GetMapping("/queryCollegeInfo")//todo 残次代码  不符合预期结果  匹配问题
    public ResponseEntity<Result> queryCollegeInfo(@RequestParam(required = false, value = "collegeName", defaultValue = "") String collegeName,
                                                   @RequestParam(required = false, value = "majorName", defaultValue = "") String majorName,
                                                   @RequestParam(required = false, value = "collegeId", defaultValue = "") String collegeId) {

        List<CollegeInfoVo> collegeInfoVoList = collegeService.queryCollegeInfo(collegeName, majorName, collegeId);
        return ResponseEntity.ok(new Result(collegeInfoVoList));

    }

    @ApiOperation("学院男女信息统计")
    @GetMapping("/info")
    public List<CollegeInfoVo> findCollegeInfo() {
        return collegeService.findCollegeInfo();
    }


    @ApiOperation("院系下拉框选择")
    @GetMapping("/getAllCollege")
    public ResponseEntity<Result> getAllCollege() {
        List<College> allCollege = collegeService.getAllCollege();
        return ResponseEntity.ok(new Result(allCollege));
    }


    @ApiOperation("列表显示学院专业信息")
    //学院 =》专业
    @GetMapping("/MajorVos")
    public ResponseEntity<Result> MajorVos() {
        List<CollegeMajorVo> collegeMajorVos = collegeService.MajorVos();
        return ResponseEntity.ok(new Result(collegeMajorVos));
    }


    @ApiOperation("院系下拉筛选框")
    @GetMapping("/map")
    public Map<String, College> findAllMap() {
        return collegeService.list().stream().collect(Collectors.toMap(College::getSid, r -> r));
    }

    @ApiOperation("查询二级学院树")
    @GetMapping("/tree")
    public ResponseEntity<Result> findTree(String adviserId) {
        List<CollegeTreeVo> collegeTreeVos = collegeService.findTree(adviserId);
        return ResponseEntity.ok(new Result(collegeTreeVos));
    }

    @ApiOperation("查询三级学院树 （根据自己的权限查看）")
    @GetMapping("/ThreeTree")
    public ResponseEntity<Result> ThreeTree(@RequestParam(defaultValue = "",required = false) String adviserId) {
        List<CollegeTreeVo> collegeTreeVos = collegeService.ThreeTree(adviserId);
        return ResponseEntity.ok(new Result(collegeTreeVos));
    }
    @ApiOperation("报道率统计")
    @GetMapping("/queryReserveCollegeVo")
    public ResponseEntity<Result> queryReserveRate(@RequestParam(required = false, value = "collegeId", defaultValue = "") String collegeId,
                                                        @RequestParam(required = false, value = "majorId", defaultValue = "") String majorId,
                                                        @RequestParam(required = false, value = "classId", defaultValue = "") String classId,
                                                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                        @RequestParam(required = false, defaultValue = "1") String type) {
        Page<ReserveCollegeVo> reserveCollegeVoPage = collegeService.queryReserveRate(pageNum, pageSize, collegeId, majorId, classId, type);
        return ResponseEntity.ok(Result.success(reserveCollegeVoPage));
    }


}


